{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "160ca35b",
   "metadata": {},
   "source": [
    "# 部署到服务器（Centos8）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d5e8b73",
   "metadata": {},
   "source": [
    "2021年10月17日21:27:54\n",
    "害~本来已经部署好了。。。\n",
    "结果selenium要用到火狐，还要安装可视化的centos。。。\n",
    "服务器有重装了系统."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1914afe",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#import\n",
    "pip install flask\n",
    "pip install pymysql\n",
    "pip install selenium\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9feeadb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#app.py 修改\n",
    "app.run(host=\"0.0.0.0\", port=\"5000\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d814fbef",
   "metadata": {},
   "source": [
    "# Mysql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5381e0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#mysql 安装https://blog.51cto.com/u_12226796/2431965\n",
    "\n",
    "#建表\n",
    "\n",
    "#bdhot\n",
    "create table `bdhot`(\n",
    "`id` int(11) not null auto_increment,\n",
    "`dt` timestamp default current_timestamp,# 默认值为当前时间\n",
    "`hotrank` int(11) default null,\n",
    "`hotscore` int(11) default null,\n",
    "`title` varchar(255) default null,\n",
    "`content` tinytext default null,\n",
    "primary key(`id`) using btree\n",
    ")engine=innodb default charset=utf8mb4;\n",
    "\n",
    "#history\n",
    "\n",
    "create table `history`(\n",
    "`ds` datetime not null comment'日期',\n",
    "`confirm` int(11) default null comment'累计确诊',\n",
    "`confirm_add` int(11) default null comment'当日新增确诊',\n",
    "`suspect` int(11) default null comment'剩余疑似',\n",
    "`suspect_add` int(11) default null comment'当日新增疑似',\n",
    "`heal` int(11) default null comment'累计治愈',\n",
    "`heal_add` int(11) default null comment'当日新增治愈',\n",
    "`dead` int(11) default null comment'累计死亡',\n",
    "`dead_add` int(11) default null comment'当日新增死亡',\n",
    "primary key(`ds`) using btree\n",
    ")engine=innodb default charset=utf8mb4;\n",
    "\n",
    "#details\n",
    "\n",
    "create table `details`(\n",
    "`id` int(11) not null auto_increment,\n",
    "`update_time` datetime not null comment'数据最后更新时间',\n",
    "`province` varchar(50) default null comment'省',\n",
    "`city` varchar(50) default null comment'市',\n",
    "`confirm` int(11) default null comment'累计确诊',\n",
    "`confirm_add` int(11) default null comment'新增确诊',\n",
    "`heal` int(11) default null comment'累计治愈',\n",
    "`dead` int(11) default null comment'累计死亡',\n",
    "primary key(`id`) using btree\n",
    ")engine=innodb default charset=utf8mb4;\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d6b2ea5",
   "metadata": {},
   "source": [
    "# geckodriver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1a7c477",
   "metadata": {},
   "outputs": [],
   "source": [
    "# download wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "664ed9eb",
   "metadata": {},
   "source": [
    "# 调试模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42ceebd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改 app.py\n",
    "\n",
    "app.run()  =>   app.run(host=0.0.0.0, port=5000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33fa81a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "python3 app.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d20f76",
   "metadata": {},
   "source": [
    "# 生产模式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0ef6b1d",
   "metadata": {},
   "source": [
    "### 服务器 gunicorn\n",
    "### 反向代理 Nginx\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8deee8d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "yum install nginx  # 安装 nginx\n",
    "pip install gunicorn # 安装 gunicorn\n",
    "gunicorn -b 127.0.0.1:5000 -D app:app  # 启动 gunicorn\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "681bf225",
   "metadata": {},
   "source": [
    "### 配置 Nginx 文件 vim /etc/nginx/nginx.conf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a14745",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sever 上添加 服务器集群 和权重 ，我只有一台服务器就写一个(127.1我写的是内网ip）。\n",
    "\n",
    "# upstream mycluster {\n",
    "#     server 127.0.0.1:5000 weight=1;\n",
    "# }\n",
    "。。。。\n",
    "\n",
    "include /etc/nginx/conf.d/*.conf;\n",
    "    upstream mycluster{\n",
    "                server 172.17.0.15:5000 weight=1;\n",
    "                }\n",
    "    server {\n",
    "        listen       80 default_server;\n",
    "        listen       [::]:80 default_server;\n",
    "        server_name  172.17.0.15;\n",
    "        root         /usr/share/nginx/html;\n",
    "。。。。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "335303c9",
   "metadata": {},
   "source": [
    "### 启动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50beef63",
   "metadata": {},
   "outputs": [],
   "source": [
    "[root@VM-0-15-centos COVID19]# gunicorn -b 172.17.0.15:5000 -D app:app\n",
    "[root@VM-0-15-centos COVID19]# ps -ef | grep gunicorn\n",
    "root       25252       1  0 16:45 ?        00:00:00 /usr/bin/python3.6 /usr/local/bin/gunicorn -b 172.17.0.15:5000 -D app:app\n",
    "root       25255   25252  5 16:45 ?        00:00:00 /usr/bin/python3.6 /usr/local/bin/gunicorn -b 172.17.0.15:5000 -D app:app\n",
    "root       25298   20928  0 16:46 pts/3    00:00:00 grep --color=auto gunicorn\n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fb14474",
   "metadata": {},
   "source": [
    "### 定时启动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf531b02",
   "metadata": {},
   "outputs": [],
   "source": [
    "[root@VM-0-15-centos COVID19]# crontab -l\n",
    "*/5 * * * * flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &'\n",
    "* */12 * * * python3 /root/COVID19/update.py up_all >> /root/COVID19/covid19.log 2>&1 &\n",
    "5 */1 * * *  python3 /root/COVID19/update.py up_hot >> /root/COVID19/covid19.log 2>&1 &\n",
    "\n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# \n",
    "[root@VM-0-15-centos COVID19]# crontab -e\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6509fba",
   "metadata": {},
   "source": [
    "### 启动 Nginx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e71a6d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "/usr/sbin/nginx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9828c09",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
